﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WUTOS.DataBase
{
    public partial class ProcessFrame : Form
    {
        #region 参数

        //线程 查找psid
        private Thread thread = null;

        //根据进程id 判断进程是否在运行 
        private int psid = 0;
        public int Psid { get { return psid; } set { psid = value; } }

        //委托 线程中调用主线程方法 
        private delegate void ReleaseSource();
 
        #endregion

        public ProcessFrame()
        {
            InitializeComponent();
            thread = new Thread(new ThreadStart(Monitor));
            thread.Start();
        }

 
        /// <summary>
        /// 线程 监视psid进程 进程结束后释放窗口
        /// </summary>
        private void Monitor()
        {
            try
            {
                while (true)
                {
                    Process[] process = Process.GetProcesses();
                    bool exit = false;
                    for (int i = 0; i < process.Length; i++)
                    {
                        if (process[i].Id == Psid)
                        {
                            exit = true;
                            break;
                        }
                    }
                    if (!exit)
                    {
                        //释放资源
                        if (this.InvokeRequired)
                        {
                            while (!this.IsHandleCreated) { }
                            ReleaseSource release = new ReleaseSource(this.thread.Abort);
                            release += timer.Dispose;
                            release += this.Close;
                            BeginInvoke(release, null);
                        }
                        else
                        {
                            if (this.IsHandleCreated)
                            {
                                thread.Abort();
                                timer.Dispose();
                                this.Close();
                                this.Dispose();
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 时间计算轴转动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            pgCricle.Value = (pgCricle.Value + 1) % 100;
            int value = Convert.ToInt32(pgCricle.Value) % 3;
            switch (value.ToString())
            {
                case "0":
                    labRun.Text = "正在运行.";
                    this.Text = "正在运行 .";
                    Thread.Sleep(100);
                    break;
                case "1":
                    labRun.Text = "正在运行 ..";
                    this.Text = "正在运行 ..";
                    Thread.Sleep(100);
                    break;
                case "2":
                    labRun.Text = "正在运行 ...";
                    this.Text = "正在运行 ...";
                    Thread.Sleep(100);
                    break;
                default:
                    break;
            }
        }
    }
}
